package com.ky.manage.activityLifecycle;

import android.app.Activity;
import android.os.Bundle;
import com.ky.manage.permission.AndPermissionUtils;
import com.ky.manage.utils.ZyUtils;

/**
 * activity栈生命周期回调
 *
 * @author zhaoyan
 * @date 2019-11-06
 */

public class CommonActivityLifecycleCallbacksImp extends EmptyActivityLifecycleCallbacks {
    private static final String TAG = "ActivityLifecycle";
    /**
     * activity计数
     */
    private int mActivityCnt = 0;
    /**
     * app是否在后台
     */
    private boolean mIsBackground = false;

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        super.onActivityCreated(activity, savedInstanceState);
        ActivityManagerUtil.getInstance().push(activity);
    }

    @Override
    public void onActivityStarted(Activity activity) {
        super.onActivityStarted(activity);
        mActivityCnt++;
        if (mActivityCnt == 1 && mIsBackground) {
            mIsBackground = false;
            //说明应用进入了前台
        }
    }

    @Override
    public void onActivityResumed(Activity activity) {
        super.onActivityResumed(activity);
        ZyUtils.getInstance().showSeeLogEntryFloatWindow(activity);
    }

    @Override
    public void onActivityPaused(Activity activity) {
        super.onActivityPaused(activity);
        ZyUtils.getInstance().hideFloatWindow(ZyUtils.SEE_LOG_ENTRY_FLOAT_WINDOW_TAG);
    }

    @Override
    public void onActivityStopped(Activity activity) {
        super.onActivityStopped(activity);
        mActivityCnt--;
        if (mActivityCnt <= 0 && !mIsBackground) {
            mIsBackground = true;
            //说明应用进入了后台
            AppBack2ForgroundUtils.getInstance().onApp2Background();
        }
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        super.onActivityDestroyed(activity);
        ActivityManagerUtil.getInstance().remove(activity);
        AndPermissionUtils.getInstance().dismissPermissionDialog();
    }
}
